from django import template
from django.utils.html import format_html

register = template.Library()


@register.simple_tag
def pre_5_page(curr_page, loop_page):
    """
    分页列只有5条
    :param curr_page:
    :param loop_page:
    :return:
    """
    curr_page = int(curr_page)
    loop_page = int(loop_page)
    offset = abs(curr_page - loop_page)
    if offset < 5:
        if curr_page == loop_page:
            page_ele = f''' <li class="paginate_button active" aria-controls="editable"
                                        tabindex="0"><a href="?page={loop_page}"
                                                        title="第{loop_page}页">{loop_page}</a></li> '''
        else:
            page_ele = f'''  <li class="paginate_button" aria-controls="editable"
                                        tabindex="0"><a href="?page={loop_page}"
                                                        title="第{loop_page}页">{ loop_page}</a></li>'''

        return format_html(page_ele)
    else:
        return ''


@register.simple_tag
def page_other_parameter(request, field, value):
    """ 获取其他参数 """
    dict_obj = request.GET.copy()
    dict_obj[field] = value
    res = dict_obj.urlencode()
    return res


'''

{% for page in pagintor_obj.paginator.page_range %}
  { {% my11 request 'page' page %}
{% endfor %}

'''
